package com.wang.vedio.player;

import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.google.gson.Gson;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.vondear.rxui.view.dialog.RxLookNumberDialog;
import com.wang.vedio.R;
import com.wang.vedio.adapter.index.InviteAdapter;
import com.wang.vedio.base.BaseActivity;
import com.wang.vedio.net.SharedHelper;
import com.wang.vedio.net.entity.BaseBean;
import com.wang.vedio.net.entity.InviteBean;
import com.wang.vedio.net.entity.InviteOrSignBean;
import com.wang.vedio.net.entity.LoginBean;
import com.wang.vedio.net.rxjava.HttpMethods;
import com.wang.vedio.net.subscribers.ProgressSubscriber;
import com.wang.vedio.net.subscribers.SubscriberOnNextListener;
import com.wang.vedio.utils.FileUtils;
import com.wang.vedio.utils.QRcodeUtils;
import com.wang.vedio.utils.statusbar.StatusBarUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class InviteFriendActivity extends BaseActivity {

    @BindView(R.id.tixianBtn)
    TextView tixianBtn;

    @BindView(R.id.qdLinear)
    View qdLinear;

    @BindView(R.id.yqlcLinear)
    View yqlcLinear;

    @BindView(R.id.btn1)
    TextView btn1;
    @BindView(R.id.btn2)
    TextView btn2;
    @BindView(R.id.btn3)
    TextView btn3;
    @BindView(R.id.btn4)
    TextView btn4;
    @BindView(R.id.btn5)
    TextView btn5;
    @BindView(R.id.btn6)
    TextView btn6;
    @BindView(R.id.btn7)
    TextView btn7;

    @BindView(R.id.vedioCount1)
    TextView vedioCount1;
    @BindView(R.id.vedioCount2)
    TextView vedioCount2;
    @BindView(R.id.vedioCount3)
    TextView vedioCount3;
    @BindView(R.id.vedioCount4)
    TextView vedioCount4;
    @BindView(R.id.vedioCount5)
    TextView vedioCount5;
    @BindView(R.id.vedioCount6)
    TextView vedioCount6;
    @BindView(R.id.vedioCount7)
    TextView vedioCount7;

    @BindView(R.id.yqrsTv)
    TextView yqrsTv;
    @BindView(R.id.yqjlTv)
    TextView yqjlTv;
    @BindView(R.id.ewmImg)
    ImageView ewmImg;
    @BindView(R.id.fzfxljTv)
    TextView fzfxljTv;
    @BindView(R.id.bcewmTv)
    TextView bcewmTv;


    @BindView(R.id.rec_main)
    RecyclerView rec_main;

    @BindView(R.id.dayTv)
    TextView dayTv;






    private TextView[] btnArgs = new TextView[7];
    private TextView[] vedioArgs = new TextView[7];

    @Override
    protected int getLayoutId() {
        return R.layout.gyss_invitefriend_layout;
    }

    private int type;

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        type = getIntent().getExtras().getInt("type");
        if(type == 0){
            setTitle("签到");
            qdLinear.setVisibility(View.VISIBLE);
            yqlcLinear.setVisibility(View.GONE);
        }else{
            setTitle("邀请好友");
            qdLinear.setVisibility(View.GONE);
            yqlcLinear.setVisibility(View.VISIBLE);
        }
        StatusBarUtil.setStatusBar(this, true, R.color.transparent, false, true);

        TextView[] btnTemp = {btn1,btn2,btn3,btn4,btn5,btn6,btn7};
        TextView[] vedioTemp = {vedioCount1,vedioCount2,vedioCount3,vedioCount4,vedioCount5,vedioCount6,vedioCount7};
        btnArgs = btnTemp;
        vedioArgs = vedioTemp;

        init();
    }

    private List<InviteBean> yqList = new ArrayList<>();
    private InviteAdapter mAdapter;
    private void init(){
        yqList.clear();
        ((SimpleItemAnimator) rec_main.getItemAnimator()).setSupportsChangeAnimations(false);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        rec_main.setLayoutManager(linearLayoutManager);
        mAdapter = new InviteAdapter(R.layout.item_invite_list, yqList);
        rec_main.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private int signDayType = -1;
    private boolean tixian;
    private String tishixx;
    private Bitmap bit;
    private String yqmUrl;
    private String userCode;
    private void getData(){
        SubscriberOnNextListener<BaseBean<InviteOrSignBean>> onNextListener = baseBean -> {
            if(type == 0){
                for(int i=0;i<baseBean.getData().getQdjlList().size();i++){
                    vedioArgs[i].setText("+"+baseBean.getData().getQdjlList().get(i).getValue());
                }
                signDayType = baseBean.getData().getSignDay();
                int signDay = baseBean.getData().getSignDay();
                if(signDay>=10){
                    signDay = signDay/10;
                    btnArgs[signDay-1].setBackgroundResource(R.drawable.btn_gray_5_shape);
                }else{
                    btnArgs[signDay-1].setBackgroundResource(R.drawable.btn_orange_5_shape);
                }
                dayTv.setText("第 "+signDay+" 天");
                for(int i=0;i<7;i++){
                    if(i<signDay-1){
                        btnArgs[i].setBackgroundResource(R.drawable.btn_gray_5_shape);
                    }else if(i>signDay-1){
                        btnArgs[i].setBackgroundResource(R.drawable.btn_gray_shape);
                    }
                }
            }
            yqList.clear();
            yqList.addAll(baseBean.getData().getInviteList());
            mAdapter.notifyDataSetChanged();

            yqrsTv.setText("已邀请："+baseBean.getData().getInviteNumber()+"人");
            yqjlTv.setText("邀请奖励： "+baseBean.getData().getInviteSyMoney()+"元");
            tixian = baseBean.getData().isTixian();
            tishixx = baseBean.getData().getTishixiaoxi();

            yqmUrl = HttpMethods.BASE_URL+"downApk.client?userCode=PLAYER_"+baseBean.getData().getUserCode();
            userCode = baseBean.getData().getUserCode();
            bit =  QRcodeUtils.qrcode(yqmUrl);
            ewmImg.setImageBitmap(bit);
        };
        Map<String, Object> params = new HashMap<>();
        params.put("memberId", SharedHelper.readId(mContext));
        params.put("typeName",type);
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(params));
        HttpMethods.getInstance().inviteOrSign(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    private int page = 1;
    private void signMethod() {
        SubscriberOnNextListener<BaseBean<LoginBean>> onNextListener = baseBean -> {
            RxToast.normal(baseBean.getMessage());
            getData();
        };
        Map<String, Object> params = new HashMap<>();
        params.put("pageIndex",signDayType);
        params.put("memberId",SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(params));
        HttpMethods.getInstance().signMethod(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    private void saveTiXian() {
        SubscriberOnNextListener<BaseBean<LoginBean>> onNextListener = baseBean -> {
            RxToast.normal(baseBean.getMessage());
            getData();
        };
        Map<String, Object> params = new HashMap<>();
        params.put("memberId",SharedHelper.readId(mContext));
        RequestBody requestBody = RequestBody.create(MediaType.parse("application/json;charset=utf-8"), new Gson().toJson(params));
        HttpMethods.getInstance().saveTiXian(new ProgressSubscriber<>(onNextListener, mContext, true), requestBody);
    }

    private void showLookNumberDialog(int lx) {
        RxLookNumberDialog dialog = new RxLookNumberDialog(this);
        dialog.getTishiTv().setText(lx==0?"您非充值会员":"提现需要实名认证");
        dialog.getBtn1().setText(lx==0?"立即开通":"去认证");
        dialog.getBtn2().setText(lx==0?"暂不开通":"暂不认证");
        dialog.getBtn_close().setVisibility(View.GONE);
        dialog.getBtn1().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(lx == 0){
                    //开通会员
                    RxActivityTool.skipActivity(mContext,VipActivity.class);
                }else{
                    //去认证
                    RxActivityTool.skipActivity(mContext,SmrzActivity.class);
                }
                dialog.dismiss();
            }
        });
        dialog.getBtn2().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //邀请好友赚会员
                dialog.dismiss();
            }
        });
        dialog.show();
    }

    @OnClick({R.id.fzfxljTv,R.id.bcewmTv,R.id.tixianBtn,R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4,R.id.btn5,R.id.btn6,R.id.btn7})
    public void onClick(View v){
        if(v.getId() == R.id.fzfxljTv){
            //复制链接
            ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
            cm.setText("#快来领新人福利,点击【"+yqmUrl+"】下载九尾狐，填写我的邀请码【"+userCode+"】，就能成为会员");
            RxToast.normal("复制成功，赶紧去分享吧！");
        }else if(v.getId() == R.id.bcewmTv){
            if(bit != null){
                File pFile = FileUtils.saveBitmapFile(bit);
                if(pFile != null){
                    Uri uri = Uri.fromFile(pFile);
                    mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
                    RxToast.normal("保存成功，赶紧去分享吧！");
                }else{
                    RxToast.normal("保存失败！");
                }
            }
        }else if(v.getId() == R.id.tixianBtn){
            if(!tixian){
                if("充值VIP".equals(tishixx)){
                    showLookNumberDialog(0);
                }else if("实名认证".equals(tishixx)){
                    //需要实名认证
                    showLookNumberDialog(1);
                    return;
                }else {
                    RxToast.normal(tishixx);
                }
                return;
            }
            //提现操作
            saveTiXian();
        } else {
            if(signDayType<10){
                if(v.getId() == btnArgs[signDayType-1].getId()){
                    //可以签到
                    signMethod();
                }
            }
        }
    }
}
